<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="主题名字" prop="seasonsName" :rules="{ required: true, message: '主题名字不能为空', trigger: 'blur'}">
          <el-input v-model="form.seasonsName" placeholder="请输入主题名字"/>
        </el-form-item>
        <el-form-item label="图标" prop="seasonsPicture" :rules="{ required: true, message: '图标不能为空', trigger: 'blur'}">
          <el-upload
            class="avatar-uploader"
            :action="uploadUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.seasonsPicture" :src="form.seasonsPicture" class="avatar" :alt="form.seasonsPicture">
            <i v-else class="el-icon-plus avatar-uploader-icon"/>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="onSubmit">立即保存</el-button>
          <el-button size="small" @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import {
  createSeasonsCategory, seasonsCategoryDetail,
  updateSeasonsCategory
} from '@/api/Category'
import { getSeasonsCategory } from '@/api/Product'

const defaultSettings = require('@/settings.js')
export default {
  data() {
    return {
      uploadUrl: defaultSettings.uploadUrl,
      categoryItem: [],
      form: {
        seasonsId: '',
        seasonsName: '',
        seasonsPicture: '',
      }
    }
  },
  created() {
    if (this.$route.query.seasonsId) {
      this.form.seasonsId = this.$route.query.seasonsId
      this.getCategory()
    }
  },
  methods: {
    cancel() {
      this.$router.push('/category/list')
    },
    getCategory() {
      seasonsCategoryDetail({ id: this.form.seasonsId }).then(res => {
        if (res.code === 200) {
          this.form.seasonsId = res.data.seasonsId
          this.form.seasonsName = res.data.seasonsName
          this.form.seasonsPicture = res.data.seasonsPicture
        } else {
          this.$message.error('网络错误')
        }
      })
    },
    onSubmit() {
      const form = {
        seasonsId: this.form.seasonsId,
        seasonsName: this.form.seasonsName,
        seasonsPicture: this.form.seasonsPicture,
        isDelete: 0
      }
      if (form.seasonsId) {
        updateSeasonsCategory(form).then(res => {
          if (res.code === 200) {
            this.$message.success('保存成功')
            this.$router.push('/category/seasons')
          } else {
            this.$message.error('网络错误')
          }
        })
      } else {
        createSeasonsCategory(form).then(res => {
          if (res.code === 200) {
            this.$message.success('保存成功')
            this.$router.push('/category/seasons')
          } else {
            this.$message.error('网络错误')
          }
        })
      }
    },
    handleAvatarSuccess(res, file) {
      this.form.seasonsPicture = res.data
    },
    beforeAvatarUpload(file) {
      const typeItem = ['image/jpeg', 'image/png']
      const isJPG = typeItem.indexOf(file.type)
      const isLt2M = file.size / 1024 / 1024 < 5
      if (isJPG < 0) {
        this.$message.error('上传图片只能是 JPG/PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 5MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 150px;
  height: 150px;
  display: block;
}
</style>
